<template>
  <div class="mod-config">
    <el-row :gutter="10">
      <el-col
        :xs="24"
        :sm="24"
        :md="10"
        :lg="11"
        :xl="11"
        style="margin-bottom: 10px"
      >
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <el-form
              :inline="true"
              :model="dataForm"
              @keyup.enter.native="getDataList()"
            >
              <el-form-item>
                <el-input
                  v-model="dataForm.key"
                  placeholder="参数名"
                  clearable
                ></el-input>
              </el-form-item>
              <el-form-item>
                <el-button @click="getDataList()">查询</el-button>
                <el-button
                  v-if="isAuth('dict:sysdict:save')"
                  type="primary"
                  @click="addOrUpdateHandle()"
                  >新增</el-button
                >
                <el-button
                  v-if="isAuth('dict:sysdict:batchDelete')"
                  type="danger"
                  @click="deleteHandle()"
                  :disabled="dataListSelections.length <= 0"
                  >批量删除</el-button
                >
              </el-form-item>
            </el-form>
            <el-table
              :data="dataList"
              border
              v-loading="dataListLoading"
              @selection-change="selectionChangeHandle"
              @current-change="handleCurrentChange"
              style="width: 100%;"
            >
              <el-table-column
                type="selection"
                header-align="center"
                align="center"
                width="50"
              >
              </el-table-column>
              <el-table-column
                prop="name"
                header-align="center"
                align="center"
                label="字典名称"
              >
              </el-table-column>
              <el-table-column
                prop="description"
                header-align="center"
                align="center"
                label="描述"
              >
              </el-table-column>
              <el-table-column
                fixed="right"
                header-align="center"
                align="center"
                width="150"
                label="操作"
              >
                <template slot-scope="scope">
                  <el-button
                    type="text"
                    size="small"
                    v-if="isAuth('dict:sysdict:update')"
                    @click="addOrUpdateHandle(scope.row.dictId)"
                    >修改</el-button
                  >
                  <el-button
                    type="text"
                    size="small"
                    v-if="isAuth('dict:sysdict:delete')"
                    @click="deleteHandle(scope.row.dictId)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              @size-change="sizeChangeHandle"
              @current-change="currentChangeHandle"
              :current-page="pageIndex"
              :page-sizes="[10, 20, 50, 100]"
              :page-size="pageSize"
              :total="totalPage"
              layout="total, sizes, prev, pager, next, jumper"
            >
            </el-pagination>
          </div>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="24" :md="14" :lg="13" :xl="13">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>字典详情</span>
            <div v-if="dataChildrenList.length > 0">
              <el-button
                v-if="isAuth('dict:sysdict:batchDelete')"
                type="danger"
                @click="dictDetailDeleteHandle()"
                class="filter-item"
                :disabled="dataChildrenListSelections.length <= 0"
                >批量删除</el-button
              >
              <el-button
                v-if="isAuth('dict:sysdict:save')"
                type="primary"
                class="filter-item"
                @click="addOrUpdateChildrenHandle()"
                >新增</el-button
              >
            </div>
          </div>
          <div v-if="dataChildrenList.length === 0">
            <div class="my-code">点击字典查看详情</div>
          </div>
          <div v-if="dataChildrenList.length > 0">
            <el-table
              :data="dataChildrenList"
              border
              v-loading="dataChildrenListLoading"
              @selection-change="selectionChildrenChangeHandle"
              style="width: 100%;"
            >
              <el-table-column
                type="selection"
                header-align="center"
                align="center"
                width="50"
              >
              </el-table-column>

              <el-table-column
                prop="dictName"
                header-align="center"
                align="center"
                label="所属字典"
              >
              </el-table-column>
              <el-table-column
                prop="label"
                header-align="center"
                align="center"
                label="字典标签"
              >
              </el-table-column>
              <el-table-column
                prop="value"
                header-align="center"
                align="center"
                label="字典值"
              >
              </el-table-column>
              <el-table-column
                prop="dictSort"
                header-align="center"
                align="center"
                label="排序"
              >
              </el-table-column>

              <el-table-column
                fixed="right"
                header-align="center"
                align="center"
                width="150"
                label="操作"
              >
                <template slot-scope="scope">
                  <el-button
                    type="text"
                    size="small"
                    v-if="isAuth('dict:sysdict:update')"
                    @click="addOrUpdateChildrenHandle(scope.row.detailId)"
                    >修改</el-button
                  >
                  <el-button
                    type="text"
                    size="small"
                    v-if="isAuth('dict:sysdict:delete')"
                    @click="dictDetailDeleteHandle(scope.row.detailId)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              @size-change="childrenSizeChangeHandle"
              @current-change="childrenCurrentChangeHandle"
              :current-page="childrenPageIndex"
              :page-sizes="[10, 20, 50, 100]"
              :page-size="childrenPageSize"
              :total="childrenTotalPage"
              layout="total, sizes, prev, pager, next, jumper"
            >
            </el-pagination>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>

    <!-- 弹窗, 新增 / 修改 -->
    <detail-add-or-update
      :detailDictId="detailDictId"
      v-if="detailAddOrUpdateVisible"
      ref="detailAddOrUpdate"
      @refreshDataList="getDataChildrenList"
    ></detail-add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./sysdict-add-or-update";
import DetailAddOrUpdate from "./sysdictdetail-add-or-update";
export default {
  data() {
    return {
      dataForm: {
        key: ""
      },
      dataChildrenList: [],
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      childrenPageIndex: 1,
      childrenPageSize: 10,
      childrenTotalPage: 0,
      dataListLoading: false,
      dataChildrenListLoading: false,
      dataListSelections: [],
      dataChildrenListSelections: [],
      addOrUpdateVisible: false,
      detailAddOrUpdateVisible: false,
      detailDictId: "",
      detailDictName:""
    };
  },
  components: {
    AddOrUpdate,
    DetailAddOrUpdate
  },
  activated() {
    this.getDataList();
  },
  methods: {
    getDataChildrenList() {
      this.dataChildrenListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/sys/dictdetail/getDictDetailList"),
        method: "get",
        params: this.$http.adornParams({
          page: this.childrenPageIndex,
          limit: this.childrenPageSize,
          dictId: this.detailDictId
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          for (let i = 0; i < data.page.list.length; i++) {
            data.page.list[i].dictName = this.detailDictName;
          }
          this.dataChildrenList = data.page.list;
          console.log("this.dataChildrenList", this.dataChildrenList);
          this.childrenTotalPage = data.page.totalCount;
        } else {
          this.dataChildrenList = [];
          this.childrenTotalPage = 0;
        }
        this.dataChildrenListLoading = false;
      });
    },
    handleCurrentChange(val) {
      console.log("val", val);
      this.detailDictId = val.dictId;
      this.detailDictName = val.name;
      this.getDataChildrenList();
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/dict/sysdict/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          key: this.dataForm.key
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    //详情每页数
    childrenSizeChangeHandle(val){
       this.childrenPageSize = val;
      this.childrenPageIndex = 1;
      this.getDataChildrenList();
    },
    //详情当前页
    childrenCurrentChangeHandle(val){
      this.childrenPageIndex = val;
      this.getDataChildrenList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    //多选
    selectionChildrenChangeHandle(val) {
      this.dataChildrenListSelections = val;
    },
    //详情新增 / 修改
    //
    addOrUpdateChildrenHandle(id) {
      debugger;
      this.detailAddOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.detailAddOrUpdate.init(id);
      });
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    //字典详情删除
     dictDetailDeleteHandle (id) {
        var ids = id ? [id] : this.dataChildrenListSelections.map(item => {
          return item.detailId
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/sys/dictdetail/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataChildrenList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.dictId;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("/dict/sysdict/delete"),
          method: "post",
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.filter-item {
  float: right;
  // display: inline-block;
  vertical-align: middle;
  margin: 0 3px 10px 0;
  input {
    height: 30.5px;
    line-height: 30.5px;
  }
}
</style>
